package com.bigdata.bookinfo.Utils;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.Reducer;

public class MyUtil {

    static int i = 0;

    public static int run(Job job, String inputPath, String outputPath
                           , Class<? extends Mapper> mapper, Class<?> v1, Class<? extends Reducer> reducer) throws Exception {

        // 第一步：设置读取文件的类：K1和V1
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path(inputPath));

        // 第二步：设置Mapper类
        job.setMapperClass(mapper);
        // 设置Map阶段的输出类型：k2和v2的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(v1);

        // 第三、四、五、六步采用默认方式（分区，排序，规约，分组）

        // 第七步：设置Reducer类
        job.setReducerClass(reducer);
        // 设置Reduce阶段的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        // 第八步：设置输出类
        job.setOutputFormatClass(TextOutputFormat.class);
        // 设置输出路径
        TextOutputFormat.setOutputPath(job,
                new Path(outputPath));

        boolean b = job.waitForCompletion(true);
        return b?1:0;
    }
}
